Add BHoMObject class to python - #233
Conversation
…properties also start JSON encoder and decoder but unsure of what to do here for decoder.
…erialiser, and improved from_json class method to use sub class initialiser to return the correct type when BHoMObject is sub classed.
…, and handle json deserialisation of lists (as BHoM serialiser in grasshopper likes to make json arrays)
…ictionary versions for converting between objects (for instance for the ladybugtools python package
…le types and fixed some bugs
…low subclassing of the BHoM serialisers, and changed to_dict() mthod to deepcopy
jamesramsden-bh
left a comment
There was a problem hiding this comment.
Tested with run_tests.bat. All relevant tests passing.
|
@BHoMBot check required |
|
@jamesramsden-bh to confirm, the following actions are now queued:
There are 350 requests in the queue ahead of you. |
|
@BHoMBot check copyright-compliance |
|
@jamesramsden-bh to confirm, the following actions are now queued:
There are 367 requests in the queue ahead of you. |
|
@BHoMBot check ready-to-merge |
|
@jamesramsden-bh to confirm, the following actions are now queued:
There are 381 requests in the queue ahead of you. |
…ich are only accessible if the file contained is imported)
|
apologies I should have added WIP to this PR |
…tor not convert strings if they are the direct return from a method
|
@Tom-Kingstone just to let you know, I have provided a |
|
@Tom-Kingstone just to let you know, I have provided a |
Issues addressed by this PR
Closes #220
This implementation will have updates in the future to iron out bugs, but for now this works well as a link between serialised c# BHoM objects and python ones.
Also included a decorator designed to be added to methods so that they can have auto-deserialised properties if calling from a BHoM adapter for instance (all that the BHoM adapter would then have to do is send a JSON document containing the BHoM Serialised json, and python would handle the rest)
The main purpose of this addition is to enable a tighter connection between BHoM adapters and Python methods/objects. Basically to add a replica of the BHoMObject class from c# into python, as well as a (fairly) simple JSON encoder/decoder that directly converts BHoM serialised JSON into python BHoMObjects (or IObject for serialised non-BHoMObjects). This implementation also comes with methods for converting to and from json and dictionaries for simplicity (rather than requiring direct usage of the json serialisers). The JSON decoder and encoder will automatically convert between PascalCase (C# side) and camel_case (python side) which should make it simpler to use it as a base class for objects that already attempt replication of existing BHoMObjects.
It is intended that the BHoMObject class (similar to c#) will act as a base class, so the json converter methods are designed to allow converting to the correct type after deserialisation.
Basic usage:
This can also be used to bring BHoM Objects from c# directly into python without needing to create a new class for them (for instance BHoM geometry can be pulled and manipulated as required with no need for a defined class)
I have added tests that check serialisation/deserialisation and round trips for IObject and BHoMObjects.
Test files
Run the python unit tests (
run_tests.bat) after build.Changelog
BHoMObjectbase class topython_toolkit.bhom.bhom_objectBHoMJSONDecoderandBHoMJSONEncoderto convert between BHoM and python objectsAdditional comments